package com.sunkingdom.qq.user.controller.impl;

import com.sunkingdom.qq.global.Result;
import com.sunkingdom.qq.user.controller.CompanionController;
import com.sunkingdom.qq.user.entity.User;
import com.sunkingdom.qq.user.service.CompanionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class CompanionControllerImpl implements CompanionController {
	private static final String USER_FRIEND_LIST = "/user/user-friend-list";
	@Autowired
	private CompanionService companionService;

	@RequestMapping(value = "/{userId}/friend", method = RequestMethod.GET)
	public String getCompanionListByUserId(@PathVariable("userId") Long userId,
			HttpServletRequest request, HttpServletResponse response) {
		List<User> companionList = companionService
				.getCompanionListByUserId(userId);
		request.setAttribute("companionList", companionList);
		return USER_FRIEND_LIST;
	}

	@RequestMapping(value = "/{userId}/friend/{companionId}", method = RequestMethod.DELETE)
	public String deleteCompanionByCompanionId(Long userId, Long companionId,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			companionService.deleteCompanionByCompanionId(userId, companionId);
			request.setAttribute(Result.MESSAGE, Result.SUCCESS);
		} catch (Exception e) {
			request.setAttribute(Result.MESSAGE, Result.FAILURE);
		}
		return Result.RESULT;
	}

}
